package com.yidu.best.stock.stockQuotation.service.impl;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.yidu.best.basicSetup.custodian.util.IDGenerator;
import com.yidu.best.stock.stockQuotation.dao.StockQuotationDao;
import com.yidu.best.stock.stockQuotation.domain.StockQuotation;
import com.yidu.best.stock.stockQuotation.domain.StockQuotationVo;
import com.yidu.best.stock.stockQuotation.service.StockQuotationService;
import com.yidu.best.stock.stockQuotation.util.ExcelUtil;
@Service
public class StockQuotationServiceImpl implements StockQuotationService{
	@Autowired
	private StockQuotationDao stockQuotationDao;
	
	@Override
	public Map<String, Object> queryStockPlateByCriteriaWithPage(StockQuotationVo stockPlateVo) {
		 int page = (stockPlateVo.getPage()-1)*stockPlateVo.getLimit(); stockPlateVo.setPage(page);
			List<StockQuotation> pageByParam = stockQuotationDao.queryStockPlateByCriteriaWithPage(stockPlateVo);
			System.out.println(pageByParam);
			int count = stockQuotationDao.selectCount(stockPlateVo);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("data",pageByParam);
			map.put("count", count);
			map.put("msg", "");
			map.put("code", 0);
			return map;
	}

	@Override
	public int selectCount(StockQuotationVo stockPlateVo) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void ajaxUploadExcel(InputStream in, MultipartFile file) throws Exception {
		List<List<Object>> listob = ExcelUtil.getBankListByExcel(in, file.getOriginalFilename());
		List<StockQuotation> stockList = new ArrayList<StockQuotation>();
		for(int i = 0;i<listob.size();i++){
			try {
				List<Object> ob = listob.get(i);
				StockQuotation stockQuotation = new StockQuotation();
				stockQuotation.setStockQuotationId(IDGenerator.getId());
				stockQuotation.setStockCode(String.valueOf(ob.get(0)));
				stockQuotation.setStockName(String.valueOf(ob.get(1)));
				stockQuotation.setIncrease(new BigDecimal(ob.get(2).toString()));
				stockQuotation.setRausAndFall(new BigDecimal(ob.get(3).toString()));
				stockQuotation.setOpeningPrice(new BigDecimal(ob.get(4).toString()));
				stockQuotation.setClosingPrice(new BigDecimal(ob.get(5).toString()));
				stockQuotation.setTradeDate(new Date().toLocaleString() );
				stockList.add(stockQuotation);
			} catch (Exception e) {
				System.out.println(e);
				continue;
			}
		}
		stockQuotationDao.addQuotation(stockList);
	}

}
